home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 21 / Mac Magazin and MacEasy Magazine CD - Issue 21.iso / Wissenschaft & Technik / yorick12vr1-ppc folder / include / readn.i < prev    next >
Text File  |  1995-07-26  |  2KB  |  57 lines

  1. /*
  2.    READN.I
  3.    Provides a handy way to read numbers from a file, skipping
  4.    non-numeric tokens.
  5.    $Id: readn.i,v 1.1 1993/08/27 18:50:06 munro Exp $
  6.  */
  7. /*    Copyright (c) 1994.  The Regents of the University of California.
  8.                     All rights reserved.  */
  9.  
  10. func raw_read_n(f, &n0, &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9)
  11. /* DOCUMENT raw_read_n, f, n0, n1, n2, ...
  12.      grabs the next numbers N0, N1, N2, ... from file F, skipping over
  13.      any whitespace, comma, semicolon, or colon delimited tokens which
  14.      are not numbers.  (Actually, only the first and last characters of
  15.      the token have to look like a number -- 4xxx3 would be read as 4.)
  16.      ***WARNING*** at most ten Ns are allowed
  17.      The Ns can be arrays, provided all have the same dimensions.
  18.    SEE ALSO: read, rdline
  19.  */
  20. {
  21.   n= numberof(n0);
  22.   line= array(string);
  23.   for (i=1 ; i<=n ; i++) {
  24.     read_n_worker, line, f, n0, i;   if (is_void(n1)) continue;
  25.     read_n_worker, line, f, n1, i;   if (is_void(n2)) continue;
  26.     read_n_worker, line, f, n2, i;   if (is_void(n3)) continue;
  27.     read_n_worker, line, f, n3, i;   if (is_void(n4)) continue;
  28.     read_n_worker, line, f, n4, i;   if (is_void(n5)) continue;
  29.     read_n_worker, line, f, n5, i;   if (is_void(n6)) continue;
  30.     read_n_worker, line, f, n6, i;   if (is_void(n7)) continue;
  31.     read_n_worker, line, f, n7, i;   if (is_void(n8)) continue;
  32.     read_n_worker, line, f, n8, i;   if (is_void(n9)) continue;
  33.     read_n_worker, line, f, n9, i;
  34.   }
  35. }
  36.  
  37. func read_n_worker(&line, f, &var, i)
  38. {
  39.   /* indirect flag necessary because can't store back into a
  40.      scalar using var(i)=... (sigh) */
  41.   if (indirect=dimsof(var)(1)) value= structof(var)();
  42.   for (;;) {
  43.     while (line) {
  44.       tok= strtok(line, ",;: \t");
  45.       line= tok(2);
  46.       len= strlen(tok(1));
  47.       if (len && strmatch("0123456789.",strpart(tok(1), len:len)) &&
  48.       (indirect? sread(tok(1), value) : sread(tok(1), var))) {
  49.     if (indirect) var(i)= value;
  50.     return;
  51.       }
  52.     }
  53.     line= rdline(f);
  54.     if (!line) error, "premature end of file in read_n";
  55.   }
  56. }
  57.